package mvc_generator.generator.car.struts

import mvc_generator.generator.MVCGeneratorGeneratorUtils
import mvc_generator.mVCGenerator.Controller
import mvc_generator.mVCGenerator.FormValidation
import mvc_generator.mVCGenerator.ModelMVC
import org.eclipse.xtext.generator.IFileSystemAccess
import mvc_generator.mVCGenerator.View

class StrutsConfigGenerator {
	def void generate(ModelMVC model, IFileSystemAccess fsa) {

		var CharSequence strutsConfig = '''''';
		var CharSequence formBeans = '''''';
		var CharSequence actionMappings = '''''';
		var int i = 0;
		var int j = 0;
		var int k = 0;

		formBeans = '''
			<form-beans>
				«FOR Controller c : model.controllers»
					«MVCGeneratorGeneratorUtils.toVoidExpr(j = 0)»
					«FOR FormValidation fv : c.formValidations»
						<form-bean name="«c.controlledView.name.toFirstUpper»ValidationForm"
							type="«StrutsGenerator.actionFormPath»«i = i + 1»" />
					«ENDFOR»
				«ENDFOR»
				</form-beans>
		'''

		//nom des actions de chaque formulaire : /actionForm«view.name»«i = i+1»
		actionMappings = '''
			<action-mappings>
			«FOR Controller c : model.controllers»
				«FOR FormValidation fv : c.formValidations»
					<action path="/actionForm«c.controlledView.name.toFirstUpper»«fv.validatedFormName.toFirstUpper»" type="«StrutsGenerator.
				servletActionPath»«c.controlledView.name.toFirstUpper»Action"
					name="«c.controlledView.name.toFirstUpper»«fv.validatedFormName.toFirstUpper»ValidationForm" scope="request" input="/pages/«c.
				controlledView.name».jsp">
					«FOR View view : fv.viewsOnSuccess»
						<forward name="«view.name»" path="/pages/«view.name».jsp" />
					«ENDFOR»
				«ENDFOR»
				</action>			
			«ENDFOR»
			</action-mappings>
		'''

		strutsConfig = '''
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
«formBeans»
«actionMappings»
	<!-- ========== Ressources de definitions de messages =========================== -->
	<message-resources parameter="Resources" />
</struts-config>		
		'''

		fsa.generateFile(model.name + "Struts" + StrutsGenerator.configPath + "struts-config.xml", strutsConfig);

	}
}
